package org.aiforum.web.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aiforum.service.ContactService;
import org.aiforum.service.PlateService;
import org.aiforum.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

/*
   * 公用父类
   * 包含Action上下文  session  cookie  request   response 提供各种方法
   * 实现ServletRequestAware,ServletResponseAware接口和方法
   * 让struts2自动将request和response注入到通用Action里面
   */
public abstract class AiActionContext  extends ActionSupport implements ServletRequestAware,ServletResponseAware{

	/**
	 * 
	 */
	//创建日志对象
	protected Logger logger=LogManager.getLogger(getClass());
	private static final long serialVersionUID = 1L;
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
      
    @Autowired
    //创建业务对象
    protected UserService userService;
    
    @Autowired
    protected PlateService plateService;
    
    @Autowired
    protected ContactService contactService;

    public void setServletResponse(HttpServletResponse response) {
    	this.response = response;
    	this.response.setContentType("UTF-8");
    }
	

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		try {
			this.request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 添加数据到session
	 */
    protected void setSession (String name,Object value){
    	request.getSession().setAttribute(name, value);
    }
    
    /*
     * 获取session中的数据
     */
    protected <T> T getSession(String name,Class<T> clz){
    	Object object=request.getSession().getAttribute(name);
    	if(null!=object){
    		return (T) object;
    	}
    	return null;
    }
    /*
     * 注销session 
     */
    protected void invalidateSession(){
    	//删除Session中的属性
    	HttpSession session=request.getSession();
    	Enumeration<String> attributeNames=session.getAttributeNames();
    	while(attributeNames.hasMoreElements()){
    		String name=attributeNames.nextElement();
    		session.removeAttribute(name);
    	}
    	//注销session
    	session.invalidate();
    }
    
    //获取sessionID
    protected String getSessionID(){
    	return request.getSession().getId();
    }
    
    /*
     * 获得请求参数
     */
    protected String getParames(String name){
    	return request.getParameter(name);
    }
    
    
    protected String getQueryString(){
    	return request.getQueryString();
    }
    
    
    protected String getHeader(String name) {
		return request.getHeader(name);
	}
    
    /*
     * 获得请求url
     */
    protected String  getRequestPath() {
		return request.getRequestURI();
	}
    
    /*
     * 获取cookie
     */
    protected String getCookie(String name){
    	Cookie[] cookies=request.getCookies();
    	for(Cookie cookie:cookies){
    		if(null!=cookie && cookie.getName().equals(name)){
    			return cookie.getValue();
    		}
    	}
    	return null;
    }
    
	/**
	 * 添加Cookie
	 * @param name
	 * @param value
	 */
	protected void setCookie(String name,String value){
		Cookie cookie = new Cookie(name, value);
		response.addCookie(cookie);
	}
	
	/**
	 * 输出数据到浏览器
	 * @param context
	 */
	protected void writer(String context){
		try {
			response.getWriter().write(context);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
